<?php
	function get_clild_list($pid)
	{
	   //开始获取子分类
		$sql_sub = "select * from ".$GLOBALS['ecs']->table('category')." where parent_id=".$pid." and is_show=1 order by sort_order asc, cat_id asc";
	
		$subres = $GLOBALS['db']->getAll($sql_sub);
		if($subres)
		{
			foreach ($subres as $sidx => $subrow)
			{
				$children[$sidx]['id']=$subrow['cat_id'];
				$children[$sidx]['name']=$subrow['cat_name'];
				$children[$sidx]['url']=build_uri('category', array('cid' => $subrow['cat_id']), $subrow['cat_name']);
				$children[$sidx]['children'] = get_clild_list($subrow['cat_id']);
			}
		}
		else 
		{
			$children = null;
		}
				
		return $children;
	}
	
	function get_recCat_list($pid,$rec=1)
	{
		$sql="select  c.* from ".$GLOBALS['ecs']->table('category')." as c left join ".$GLOBALS['ecs']->table('cat_recommend')." as cr on c.cat_id=cr.cat_id  where  c.parent_id=".$pid." and cr.recommend_type='".$rec."' order by c.sort_order asc, c.cat_id asc";
		$res = $GLOBALS['db']->getAll($sql);
		$list=array();
		foreach ($res as $sidx => $row)
		{
			$list[$row['cat_id']]['id']=$row['cat_id'];
			$list[$row['cat_id']]['name']=$row['cat_name'];
			$list[$row['cat_id']]['url']=build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
		}
		return $list;
	}
	
	/**
	 * 通过类型与传入的ID获取广告内容
	 *
	 * @param string $type
	 * @param int $id
	 * @return string
	 */					
	function get_adv($type,$id)
	{
		 $sql = "select ap.ad_width,ap.ad_height,ad.ad_name,ad.ad_code,ad.ad_link from ".$GLOBALS['ecs']->table('ad_position')." as ap left join ".$GLOBALS['ecs']->table('ad')." as ad on ad.position_id = ap.position_id where ad.ad_name='".$type."_".$id."' and ad.media_type=0 and UNIX_TIMESTAMP()>ad.start_time and UNIX_TIMESTAMP()<ad.end_time and ad.enabled=1";
		 $res = $GLOBALS['db']->getRow($sql);
		
		 if($res)                        
		 return  "<a href='".$res['ad_link']."' target='_blank'><img src='data/afficheimg/".$res['ad_code']."' width='".$res['ad_width']."' height='".$res['ad_height']."' /></a>";	
		 else
		 {
			return "";
		  }  
	}
	
	/*
	 *获取某广告位名称的广告列表
	 */
	function get_cat_advlist($position)
	{
	   $arr=array();
	   $sql = "select ap.ad_width,ap.ad_height,ad.ad_name,ad.ad_code,ad.ad_link,ad.ad_id from ".$GLOBALS['ecs']->table('ad_position')." as ap left join ".$GLOBALS['ecs']->table('ad')." as ad on ad.position_id = ap.position_id where ap.position_name='".$position."' and ad.media_type=0 and UNIX_TIMESTAMP()>ad.start_time and UNIX_TIMESTAMP()<ad.end_time and ad.enabled=1";
		 $res = $GLOBALS['db']->getAll($sql);
		 foreach($res as $idx => $row)
		 {
			$arr[$row['ad_id']]['name'] = $row['ad_name'];
			$arr[$row['ad_id']]['ad_link'] = $row['ad_link'];
			$arr[$row['ad_id']]['ad_code'] = $row['ad_code'];
			$arr[$row['ad_id']]['ad_width'] = $row['ad_width'];
			$arr[$row['ad_id']]['ad_width'] = $row['ad_height'];
			$arr[$row['ad_id']]['content'] = "<a href='".$row['ad_link']."' title='". $row['ad_name']."' target='_blank'><img src='data/afficheimg/".$row['ad_code']."' width='".$row['ad_width']."' height='".$row['ad_height']."' /></a>";
		 }
		 return $arr;
	}
	
	//获取分类文章
	function get_recpronew()
	{
		$sql="select a.article_id, a.title,a.file_url, a.open_type from  ".$GLOBALS['ecs']->table('article')." as a left join  ".$GLOBALS['ecs']->table('article_cat')." as b on a.cat_id=b.cat_id where b.cat_name='精品特购' order by a.article_id desc limit 1";
		$row = $GLOBALS['db']->getRow($sql);
		if($row)
		{
			$row['url']= $row['open_type'] != 1 ? build_uri('article', array('aid'=>$row['article_id']), $row['title']) : trim($row['file_url']);
			echo '<a href="'.$row['url'].'" title="'.$row['title'].'">'.sub_str($row['title'],16).'</a>';
		}
	}
	
	/*获取商品下的文章*/
	
	function get_rel_art($catid,$limit=6)
	{
		$cats = get_children($catid);
		$sql = "select a.* from ".$GLOBALS['ecs']->table('article')." as a left join ".$GLOBALS['ecs']->table('goods_article')." as ga on a.article_id=ga.article_id left join ".$GLOBALS['ecs']->table('goods')." as g on ga.goods_id=g.goods_id where ".$cats." order by a.article_id desc limit ".$limit;
		
		$res=$GLOBALS['db']->getAll($sql);
		$list=array();
		foreach($res as $idx => $row )
		{
			$list[$row['article_id']]['id']    = $row['article_id'];
			$list[$row['article_id']]['title'] = $row['title'];
			$list[$row['article_id']]['url']   = $row['open_type'] != 1 ? build_uri('article', array('aid'=>$row['article_id']), $row['title']) : trim($row['file_url']);
			$list[$row['article_id']]['catename'] =$GLOBALS['db']->getOne("select cat_name from ".$GLOBALS['ecs']->table('article_cat')." where cat_id=".$row['cat_id']." ");
			$list[$row['article_id']]['cateurl'] =build_uri('article_cat', array('acid'=>$row['cat_id']));
		}
		return $list;
	}
	
?>